package com.jxc.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jxc.dao.ResourceDaoI;
import com.jxc.model.po.Resource;
import com.jxc.model.vo.VResource;

@Service("resourceDao")
public class ResourceDaoImpl implements ResourceDaoI {
	@Autowired
	private SessionFactory sessionFactory;

	@Override
	public List<VResource> list() {
		String hql = "from Resource";
		Session session = sessionFactory.openSession();
		List<Resource> list = session.createQuery(hql).list();
		List<VResource> result = changeModel(list);
		session.close();
		return result;
	}

	private List<VResource> changeModel(List<Resource> resources) {
		List<VResource> l = new ArrayList<VResource>();
		if (resources != null && resources.size() > 0) {
			for (Resource t : resources) {
				VResource a = new VResource();
				BeanUtils.copyProperties(t, a);
				if (t.getParent() != null) {
					a.setPid(t.getParent().getId());
					a.setPname(t.getParent().getName());
				}
				l.add(a);
			}
		}
		return l;
	}
}
